﻿using static RAP.Framework.Libary.DataSource.ServiceUtils;
using RAP.Framework.Libary.Utils;
using System.Collections.Generic;

namespace RAP.Framework.Libary.SqlTaskParser.Parsers
{
	/// <summary>
	/// @param解析器，将指定名称的结果集作为条件参数
	/// </summary>
	public class ResultParamTokenParser : TokenParserBase
  {
    public override void ParseToken( SqlParseContext parseContext )
		{
			//将当前并行任务集合添加到所有任务集合队列中
			var tokenParam = parseContext.CurrentToken.Param?.Trim();
			parseContext.CurrentParallelTasks.ResultParams = GetResultParams( tokenParam );
      base.ParseToken(parseContext);
    }

		protected virtual IList<SqlTaskResultParam> GetResultParams( string tokenParam )
		{
			IList<SqlTaskResultParam> resultParams = null;
			if ( tokenParam.IsNotNullOrEmpty() )
			{
				resultParams = new List<SqlTaskResultParam>();
				tokenParam.Split( ',' ).Map<string>( name =>
				{
					if ( name.IsNotNullOrEmpty() )
					{
						var resultName = name.LeftSubstring( AS_SYNTAX, name );
						var paramName = name.RightEndString( AS_SYNTAX, name );
						resultParams.Add( new SqlTaskResultParam
						{
							ResultName = resultName.Trim(),
							Alias = paramName.Trim()
						} );
					}
				} );
			}
			return resultParams;
		}
	}
}
